 
*File to merge NOAA weather data and EPA PM2p5 pollution data to game user data

***Inputs:
* $Data/GameUserData.dta, 
* $Data/GameUserweighted_ISDweather_zd.dta
* $Data/GameUserweighted_pollution_zd.dta
* $Data/Laggeddailyweather_zd.dta
* $Data/Laggeddailypm_zd.dta

***Output:
* $Data/GameUserISDWeatherPollution_daily.dta

use $Data/GameUserData.dta , clear


	merge m:1 zip date using $Data/GameUserweighted_ISDweather_zd.dta
	drop if _merge==2
	drop _merge


	merge m:1 zip date using $Data/GameUserweighted_pollution_zd.dta
	drop if _merge ==2
	drop _merge 


	sort anon_id nth_play 
		 
	keep if 	 weighted_temperature <. & (median_closest <. | weighted_calm<.)  & weighted_dailypm25 <.
		 
	
	 duplicates tag anon_id time_stamp, gen(dup)
	 
	 by anon_id time_stamp (nth_play), sort: gen todrop = _n>1
	 
	 drop if todrop ==1
	 
	 drop dup 
	  duplicates tag anon_id time_stamp, gen(dup)

	  drop dup 
	  drop todrop
	 
	 xtset anon_id time_stamp	 
	
	** add lags 
	
	forval l = 1(1)7 {
	
	merge m:1  zip date using $Data/Lag`l'dailyweather_zd.dta
	drop if _merge==2
	drop _merge
	
	
	merge m:1  zip date using $Data/Lead`l'dailyweather_zd.dta
	drop if _merge==2
	drop _merge
	
	
	merge m:1 zip date using $Data/Lag`l'dailypm_zd.dta
	drop if _merge==2	 
	drop _merge 
	
	merge m:1 zip date using $Data/Lead`l'dailypm_zd.dta
	drop if _merge==2	 
	drop _merge 
	}
	
	
save $Data/GameUserISDWeatherPollution_daily.dta, replace




	




